using System.Collections;
using TMPro;
using UnityEngine;

public class PowerTimer : MonoBehaviour
{
	private float timer;

	[SerializeField]
	private ParticleSystem _start;

	[SerializeField]
	private ParticleSystem _finish;

	public Transform _head;

	private Upgrade _u;

	private void Start()
	{
		_u = Object.FindObjectOfType<Upgrade>();
	}

	public void StartTimer(float t)
	{
		if (timer > 0.1f)
		{
			timer += t;
			return;
		}
		StopAllCoroutines();
		_start.Play();
		_u.MaxPower();
		timer = t;
		StartCoroutine(SpendTime());
	}

	public void FinishPower()
	{
		StopAllCoroutines();
		_u.CurrentPower();
	}

	private IEnumerator SpendTime()
	{
		while (timer > 0f)
		{
			if ((bool)_head)
			{
				base.transform.position = _head.position;
			}
			timer -= Time.fixedDeltaTime;
			yield return new WaitForFixedUpdate();
		}
		_finish.Play();
		FinishPower();
	}
}
